package com.hzbank.juc.biased;

import lombok.extern.slf4j.Slf4j;
import org.openjdk.jol.info.ClassLayout;

import java.util.Vector;
import java.util.concurrent.locks.LockSupport;

/**
 * @description:
 * @author: libugu@cskaoyan.com
 * @date 2023/12/21 - 15:31
 */
@Slf4j
public class BiasedLock03 {
    static Thread t1,t2,t3;
    public static void main(String[] args) throws InterruptedException {

            Vector<Dog> list = new Vector<>();

            int loopNumber = 39;
            t1 = new Thread(() -> {
                for (int i = 0; i < loopNumber; i++) {
                    Dog d = new Dog();
                    list.add(d);
                    synchronized (d) {
                        log.debug(i + "\t" + ClassLayout.parseInstance(d).toPrintable());
                    }
                }
                LockSupport.unpark(t2);
            }, "t1");
            t1.start();

            t2 = new Thread(() -> {
                LockSupport.park();
                log.debug("===============> ");
                for (int i = 0; i < loopNumber; i++) {
                    Dog d = list.get(i);
                    log.debug(i + "\t" + ClassLayout.parseInstance(d).toPrintable());
                    synchronized (d) {
                        log.debug(i + "\t" + ClassLayout.parseInstance(d).toPrintable());
                    }
                    log.debug(i + "\t" + ClassLayout.parseInstance(d).toPrintable());
                }
                LockSupport.unpark(t3);
            }, "t2");
            t2.start();

            t3 = new Thread(() -> {
                LockSupport.park();
                log.debug("===============> ");
                for (int i = 0; i < loopNumber; i++) {
                    Dog d = list.get(i);
                    log.debug(i + "\t" + ClassLayout.parseInstance(d).toPrintable());
                    synchronized (d) {
                        log.debug(i + "\t" + ClassLayout.parseInstance(d).toPrintable());
                    }
                    log.debug(i + "\t" + ClassLayout.parseInstance(d).toPrintable());
                }
            }, "t3");
            t3.start();

            t3.join();
            log.debug(ClassLayout.parseInstance(new Dog()).toPrintable());
    }
}
